package com.linkwechat.quartz.task;

import com.linkwechat.wecom.client.WeKfClient;
import com.linkwechat.wecom.domain.dto.WeResultDto;
import com.linkwechat.wecom.domain.dto.kf.AccountListDto;
import com.linkwechat.wecom.domain.dto.kf.AccountListResDto;
import com.linkwechat.wecom.domain.dto.kf.OpenKfidDto;
import com.linkwechat.wecom.mapper.*;
import com.linkwechat.wecom.service.IWeAccessTokenService;
import com.linkwechat.wecom.service.IWeMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2022-7-18.
 */

@Slf4j
@Component("DeleteWeworkKfAccountTask")
public class DeleteWeworkKfAccountTask {
    @Autowired
    WeKfClient weKfClient;

    @Autowired
    private IWeAccessTokenService iWeAccessTokenService;

    @Autowired
    private IWeMaterialService materialService;

    @Resource
    WeworkKfAccountMapper weworkKfAccountMapper;

    @Resource
    WeCorpAccountMapper weCorpAccountMapper;

    @Resource
    WeUserMapper weUserMapper;

    @Resource
    WeworkKfMemberRelationMapper weworkMemberRelationMapper;

    @Resource
    WeworkKfServicerMapper weworkKfServicerMapper;

    public void deleAllNotExists() {
        log.info("商家客服账号删除>>>>>>>>>>>>>>>>>>>启动");

        int start=0;
        int num=100;
        AccountListDto accountListDto = new AccountListDto();
        accountListDto.setLimit(num);
        OpenKfidDto openKfidDto = new OpenKfidDto();
        Map param = new HashMap<>();

        int index=0;
        while(start<=5000) {
            accountListDto.setOffset(start);
            AccountListResDto accountListResDto = weKfClient.getAccountList(accountListDto);
            for(AccountListResDto.Account account: accountListResDto.getAccountList()){
                String openKfid = account.getOpenKfid();
                param.put("open_kfid", openKfid);
                List accounts = weworkKfAccountMapper.selectByMap(param);
                //if(accounts.size()==0){
                    openKfidDto.setOpen_kfid(openKfid);
                    WeResultDto weResultDto = weKfClient.delAccount(openKfidDto);
                    if(weResultDto.getErrcode()!=0){
                        System.out.println("删除客服账户报错：" + weResultDto.getErrmsg());
                    }

                    index++;
                //}
            }

            start+=100;
        }

        System.out.println("需删除客服账户数：" + index);


        log.info("商家客服账号删除>>>>>>>>>>>>>>>>>>>启动");

    }

}
